package com.example.ll_video.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.ll_video.MyApplication;
import com.example.ll_video.R;
import com.example.ll_video.bean.ResultData;
import com.example.ll_video.bean.UserInfo;
import com.example.ll_video.constant.Constant;
import com.example.ll_video.utils.HttpUtils;
import com.example.ll_video.utils.JsonUtil;
import com.example.ll_video.utils.SPUtils;
import com.example.ll_video.utils.TelephoneUtil;
import com.example.ll_video.utils.ToastUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.xwray.passwordview.PasswordView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_password)
    PasswordView etPassword;
    @BindView(R.id.login_btn)
    TextView loginBtn;
    @BindView(R.id.register_btn)
    TextView registerBtn;
    private QMUITipDialog qmuiTipDialog;

    @Override
    public void onCreate() {
        contentView(R.layout.login_activity_math);
        ButterKnife.bind(this);

        String userName = getIntent().getStringExtra("userName");
        if (!TextUtils.isEmpty(userName))
            etAccount.setText(userName);

        qmuiTipDialog = new QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .create(false);

        setTitle("登录");
        showBackImage(view -> finish());

    }
    @OnClick({R.id.login_btn, R.id.register_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn:
                loginBtn.setEnabled(false);
                login();
                break;
            case R.id.register_btn:
                Intent intent = new Intent(context, RegisterActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void login() {
        String userName = etAccount.getText().toString().trim();
        if (TextUtils.isEmpty(userName)) {
            ToastUtils.showTextToast(context, "请输入电话号码");
            return;
        } else {
            TelephoneUtil.checkCellphone(userName);
            if (!TelephoneUtil.checkCellphone(userName)) {
                ToastUtils.showTextToast(context, "请输入正确的电话号码");
                return;
            }
        }

        String passwords = etPassword.getText().toString().trim();
        if (TextUtils.isEmpty(passwords)) {
            ToastUtils.showTextToast(context, "请输入密码");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("account", userName);
        map.put("password", passwords);
        HttpUtils.post(Constant.LOGIN, map, new HttpUtils.HttpCallBack() {
            @Override
            public void onSuccess(ResultData resultData) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        loginBtn.setEnabled(true);

                        if (!resultData.getMsg().equals("err")) {
                            JSONObject jsonObject = JSON.parseObject(resultData.getData());
                            UserInfo userInfo1 = JsonUtil.jsonToObject(jsonObject.getString("userinfo"), UserInfo.class);
                            MyApplication.getInstance().setUserInfo(userInfo1);
                            SPUtils.put(context, "userInfo", resultData.getData());
                            ToastUtils.showTextToast(context, "登录成功");
                            Intent intent = new Intent(context, NewMainActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            if (TextUtils.isEmpty(resultData.getMsg()))
                                ToastUtils.showTextToast(context, resultData.getMsg());
                            else ToastUtils.showTextToast(context, "登录失败");
                        }

                    }
                });
            }

            @Override
            public void onFail(String error) {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        loginBtn.setEnabled(true);

                        ToastUtils.showTextToast(context, "登录失败，请重试");
                    }
                });
            }
        });
    }
}
